<?php
/**
 * User: hjd-yao
 * Date: 2018/12/17
 * Time: 9:40
 */

namespace Develop\Models;

use App\Models\System\AuthModel as BaseAuthModel;
use App\Helpers\SelfRoutes;
use App\Models\System\MenuModel;

class AuthModel extends BaseAuthModel
{
    public function getRouteList()
    {
        $all_routes = SelfRoutes::make()->setRoutes()->filter(['name','controller','title','method'])->getAllInfo();

        $model_routes = self::get(['action','relate_action'])->toArray();
        $has_routes = array_column($model_routes,'action','action');

        foreach ($model_routes as $item){
            if($item['relate_action']){
                $relate_action = str2arr($item['relate_action']);
                if(is_array($relate_action)){
                    foreach ($relate_action as $action){
                        $has_routes[$action] = $action;
                    }
                }
            }
        }
        $need_routes =array_diff_key($all_routes,$has_routes);
        return $need_routes;
    }

    public function getPostActions($except = [])
    {
        $all_routes = SelfRoutes::make()->setRoutes()->filterByKey('method','POST')
            ->filter(['name','controller','title','method'])->getAllInfo();
        $model_routes = self::get(['action','relate_action'])->toArray();
        $has_routes = array_column($model_routes,'action','action');
        foreach ($model_routes as $item){
            if($item['relate_action']){
                $relate_action = str2arr($item['relate_action']);
                if(is_array($relate_action)){
                    foreach ($relate_action as $action){
                        if(!$except||!in_array($action,$except)){
                            $has_routes[$action] = $action;
                        }
                    }
                }
            }
            if($except&&in_array($item['action'],$except)){
                unset($has_routes[$item['action']]);
            }
        }
        $need_routes =array_diff_key($all_routes,$has_routes);
        return $need_routes;
    }

    public function addAction($params)
    {
        $parent_id = 0;
        $parent_ids = '';
        if(isset($params['action_parents'])){
            $parents = $this->handleParents($params['action_parents']);
            $parent_id = $parents['parent_id'];
            $parent_ids = $parents['parent_ids'];
        }
        $data['name'] = $data['action']= $params['action_name'];
        $data['title'] = $params['action_title'];
        $data['auth_type'] = $params['action_type'];
        $data['module_name'] = $params['module_name'];
        $data['relate_action'] = $params['relate_action'];
        $data['parent_id'] = $parent_id;
        $data['parent_ids'] = $parent_ids;
        $data['check_safe'] = 0;
        $data['write_log'] = 0;
        $data['set_menu'] = $params['set_menu']??0;
        $data['sort'] = $params['sort']??1000;
        $data['status'] = $params['status']??1;
        $data['group'] = SelfRoutes::make()->setRoutes()->getActionGroup($data['action']);
        $data['created_at'] = time();
        $data['created_ip'] = get_client_ip(1);
        if($id = self::insertGetId($data)){
            if(in_array(intval($data['auth_type']),[1,2])&&$data['set_menu']>0){
                $params['auth_id'] = $id;
                MenuModel::init()->addMenu($this->getMenuParams($params));
            }
            return true;
        }
        return false;
    }

    private function getMenuParams($params)
    {
        $data['url'] = $params['action_name'];
        $data['title'] = $params['action_title'];
        $data['parent_ids'] = $params['menu_parents'];
        $data['icon'] = $params['menu_icon'];
        $data['sort'] = $params['sort'];
        $data['auth_id'] = $params['auth_id'];
        return $data;
    }

    public function updateAction($data,$action_id)
    {
        $action = self::find($action_id);
        if(isset($data['action_title'])){
            $action->title = $data['action_title'];
        }
        if(isset($data['action_parents'])){
            $parents = $this->handleParents($data['action_parents']);
            $action->parent_id = $parents['parent_id'];
            $action->parent_ids = $parents['parent_ids'];
        }
        if(isset($data['action_type'])){
            $action->auth_type = $data['action_type'];
        }

        if(isset($data['module_name'])){
            $action->module_name = $data['module_name'];
        }
        if(isset($data['relate_action'])){
            $action->relate_action = $data['relate_action'];
        }
        if(isset($data['sort'])){
            $action->sort = $data['sort'];
        }
        if(isset($data['status'])){
            $action->status = $data['status'];
        }

        $action->updated_at = time();
        $action->updated_ip = get_client_ip(1);
        return $action->save();
    }

    private function handleParents($action_parents)
    {
        $parent_id = 0;
        $parent_ids = '';
        if($action_parents){
            $pids = array_filter(str2arr($action_parents));
            if(is_array($pids)&&!empty($pids)){
                $parent_id = end($pids);
                $parent_ids = arr2str($pids);
            }elseif(!is_array($pids)&&!empty($pids)){
                $parent_id = $pids;
                $parent_ids = $pids;
            }
        }

        return ['parent_id'=>$parent_id,'parent_ids'=>$parent_ids];
    }
}